#encoding=utf-8
import redis
from rediscluster import StrictRedisCluster
import sys
import re
import matplotlib.pyplot as plt
def redis_cluster():
    redis_nodes =  [{'host':'10.100.31.209','port':30001},
                    {'host':'10.100.31.209','port':30002},
                    {'host':'10.100.31.209','port':30003},
                    {'host':'10.100.31.209','port':30004},
                    {'host':'10.100.31.209','port':30005},
                    {'host':'10.100.31.209','port':30006},
                    {'host':'10.100.31.209','port':30007}
                   ]
    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except Exception,e:
        print "Connect Error!"
        sys.exit(1)

    redisconn.set('name','admin')
    # redisconn.rpush("age1",1)
    # redisconn.rpush("age1",2,3)
    # redisconn.rpush("age1",4,5,)
    redisconn.rpush("age1",1,2,3,4,5,6)

    # redisconn.set('age',18)
    print "name is: ", redisconn.get('name')
    print "age  is: ", redisconn.llen('javajob')

# redis_cluster()
def getleable(item):
    # labels = '3-4k', '4-5k', '5-6k', '6-7k', '7-8k', '8-9k', '9-10k', '10-11k', '11-12k', '12-13k', '13-14k',\
    #          '14-15k', '15-16k', '16-17k', '17-18k', '18-19k', '19-20k', '20-21k', '21-22k', '22-23k', '23-24k'
    labels = '3-5k', '5-7k', '7-9k', '9-11k', '11-13k', '13-15k', '15-17k', '17-19k', '19-21k', '21-23k', '23-25k'

    sizes = []
    explode = []
    # for k in range(3,24):
    k=3
    while k <25:
     sizes.append(int(count_num_func(item,k))+int(count_num_func(item,k+1)))
     explode.append(0)
     k=k+2
     # explode = (0, 0.1, 0, 0, 0, 0, 0)

    fig, ax = plt.subplots()
    ax.pie(sizes, explode=explode, labels=labels, autopct='%.2f%%', shadow=True, startangle=90)
    fig.set_size_inches(8, 8)
    plt.show()
def get_redis():
    r = redis.Redis(host = 'localhost', port = 6379, db = 0)
#往redis中写数据
#查看对应的值
    print("javajob3的长度为",r.llen("python"))
    str=r.lrange("python", 0, r.llen("python"))
    list = []
    for m in str :
     try:
      # if cmp(re.findall(r"薪资:(.+?)职位", m)[0].strip() , '薪资面议')!= 0 or cmp(re.findall(r"薪资:(.+?)职位", m)[0].strip() , '校招')!=0:
      #  print re.findall(r"薪资:(.+?)职位", m)[0]
       sery=re.findall(r"薪资:(.+?)职位", m)[0]
       item= re.findall(u"(.*?)K-(.*?)K",sery)#取出薪资
       if item:
        # print int(float(item[0][0])+float(item[0][1])//2)
        list.append(int(float(item[0][0])+float(item[0][1])//2))
        # print(int(float(item[0][0])+float(item[0][1])//2))#求平均值
     except Exception,e:
        print e
        sys.exit(1)
    return list
def count_num_func(num_list,num):
  '''''
  计数指定数字
  '''
  # split_list=[]
  # for one in num_list:
  #   split_list+=list(str(one))
  print num, num_list.count(num)
  return num_list.count(num)
getleable(get_redis())